package UIFree.controls.base
{
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class uif_previewSource extends uif_source {
		private var _mask	: uif_ecomponent;
		private var _url	: String;
		private var _index	: int; 
		
		public function uif_previewSource(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			alpha = 0;
			_index = scrollArea.scrollArea.numChildren - 1;
		}
		
		public function get scrollArea(): uif_hscrollArea {
			return (parent.parent.parent as uif_hscrollArea)
		}
		
		protected function doLoadFromScroll(e: Event): void {
			if (scrollArea.checkVisible(_index)) {
				loadSource(_url);
				scrollArea.removeEventListener(Event.SCROLL, doLoadFromScroll);
			}
		}
		
		override public function loadSource(url: String, onComplete: Function=null): void {
			_url = url;
			var s: int = -scrollArea.startIndex * scrollArea.rows;
			if (scrollArea.checkVisible(_index)) super.loadSource(_url, onComplete);
			else addEvent(Event.SCROLL, doLoadFromScroll, scrollArea);
		}
		
		override protected function doLoad(e:Event):void {
			if (parent) {
				if (parent.hasOwnProperty('fitToInner')) {
					if (!_mask) {
						var inner: Rectangle = getInner();
						_mask = new uif_ecomponent(this, inner.x, inner.y, inner.width, inner.height, 'white');
					}
					if ((_source.width < _mask.width) || 
						(_source.height < _mask.height)) {
						var kof: Number = _source.height / _source.width;
						if (kof > 1) {
							_source.width  = _mask.width;
							_source.height = _mask.width * kof;
						} else {
							_source.height = _mask.width;
							_source.width  = _mask.width / kof;
						}
					}
					_source.x = _mask.x + (_mask.width  - _source.width) / 2; 
					_source.y = _mask.y + (_mask.height - _source.height) / 2; 
					_source.mask = _mask;
				} else super.doLoad(e);
				slideShow();
			}
			//fx('scaleCenter', 0.1, 1).start();
		}
	}
}